﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using sqlserverextensions.Helper;


    public partial class UserDefinedFunctions
    {
        
        /// <summary>
        /// Checks if the parameter "value" contains the parameter "filter"
        /// </summary>
        /// <param name="value">Value which will be searched</param>
        /// <param name="filter">This parameter filters the parameter value.</param>
        /// <returns>
        /// BIT data type as an boolean expression.
        /// 1 = true
        /// 0 = false
        /// </returns>
        /// <example>Example
        /// <code>
        /// IF DbExt.[Contains]('Munich lost champions league final', 'league') = 1
	    ///     SELECT '"league" was found'
        /// </code>
        /// </example>
        [Microsoft.SqlServer.Server.SqlFunction]
        public static SqlBoolean Contains(SqlString value, SqlString filter)
        {
            Int16 errSeverity = 16;

            try
            {
                if (value.IsNull)
                    value = "";

                return value.Value.Contains(filter.Value);
            }
            catch (Exception ex)
            {
                ExceptionHelper.EvaluateException(ex, errSeverity);
                return false;
            }
        }
    }

